package com.lagou.ex.mybatis.util;

import com.lagou.ex.mybatis.common.model.resp.BaseResp;

/**
 * 响应结果类
 */
public final class ResultUtil {
    public final static String RET_CODE_SUCCESS = "200";
    public final static String RET_CODE_FAIL = "400";
    public final static String RET_MSG_SUCCESS = "成功";
    public final static String RET_MSG_FAIL = "失败";

    public static <T extends BaseResp> T buildSuccess(T t) {
        t.setRetCode(RET_CODE_SUCCESS);
        t.setRetMsg(RET_MSG_SUCCESS);
        return t;
    }

    public static <T extends BaseResp> T buildFail(T t) {
        t.setRetCode(RET_CODE_FAIL);
        t.setRetMsg(RET_MSG_FAIL);
        return t;
    }

    public static <T extends BaseResp> T build(Class<T> cls, String retCode, String retMsg) {
        T t = null;
        try {
            t = cls.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        t.setRetCode(retCode);
        t.setRetMsg(retMsg);
        return t;
    }

    public static <T extends BaseResp> T build(Class<T> cls, boolean flag) {
        T t = null;
        try {
            t = cls.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        if (flag) {
            t.setRetCode(RET_CODE_SUCCESS);
            t.setRetMsg(RET_MSG_SUCCESS);
        } else {
            t.setRetCode(RET_CODE_FAIL);
            t.setRetMsg(RET_MSG_FAIL);
        }

        return t;
    }

}
